

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Tutorial: Histogram - Boost.GIL  documentation</title>
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/style.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
          URL_ROOT:    '../',
          VERSION:     '',
          COLLAPSE_MODINDEX: false,
          FILE_SUFFIX: '.html'
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="top" title="Boost.GIL  documentation" href="../index.html" />
    <link rel="next" title="Tutorial: Image Gradient" href="gradient.html" />
    <link rel="prev" title="Tutorial: Video Lecture" href="video.html" /> 
  </head>
  <body>
    <div class="header">
    <table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
    "header">
      <tr>
        <td valign="top" width="300">
          <h3><a href="../index.html"><img
          alt="C++ Boost" src="../_static/gil.png" border="0"></a></h3>
        </td>

        <td >
          <h1 align="center"><a href="../index.html"></a></h1>
        </td>
	<td>
      <div id="searchbox" style="display: none">
        <form class="search" action="../search.html" method="get">
          <input type="text" name="q" size="18" />
          <input type="submit" value="Search" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
      </div>
      <script type="text/javascript">$('#searchbox').show(0);</script>
	</td>
      </tr>
    </table>
    </div>
    <hr/>
    <div class="content">
    <div class="navbar" style="text-align:right;">
      
      
      <a class="prev" title="Tutorial: Video Lecture" href="video.html"><img src="../_static/prev.png" alt="prev"/></a>
      <a class="next" title="Tutorial: Image Gradient" href="gradient.html"><img src="../_static/next.png" alt="next"/></a>
      
    </div>
      
  <div class="section" id="tutorial-histogram">
<h1>Tutorial: Histogram</h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#original-implementation" id="id1">Original implementation</a></li>
<li><a class="reference internal" href="#gil-implementation" id="id2">GIL implementation</a></li>
</ul>
</div>
<p>This is a short tutorial presenting an example of a very simple sample of code
from an existing code base that calculates histogram of an image.
Next, the program is rewritten using GIL featres.</p>
<div class="section" id="original-implementation">
<h2><a class="toc-backref" href="#id1">Original implementation</a></h2>
<p>Actual code from a commercial software product that computes the luminosity
histogram (variable names have been changed and unrelated parts removed):</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="kt">void</span> <span class="nf">luminosity_hist</span><span class="p">(</span>
    <span class="n">std</span><span class="o">::</span><span class="kt">uint8_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">r</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="kt">uint8_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">g</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="kt">uint8_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">b</span><span class="p">,</span>
    <span class="kt">int</span> <span class="n">rows</span><span class="p">,</span> <span class="kt">int</span> <span class="n">cols</span><span class="p">,</span> <span class="kt">int</span> <span class="n">sRowBytes</span><span class="p">,</span> <span class="n">Histogram</span><span class="o">*</span> <span class="n">hist</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">r</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">r</span> <span class="o">&lt;</span> <span class="n">rows</span><span class="p">;</span> <span class="n">r</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">c</span> <span class="o">&lt;</span> <span class="n">cols</span><span class="p">;</span> <span class="n">c</span><span class="o">++</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="kt">int</span> <span class="n">v</span> <span class="o">=</span> <span class="n">RGBToGray</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="n">c</span><span class="p">],</span> <span class="n">g</span><span class="p">[</span><span class="n">c</span><span class="p">],</span> <span class="n">b</span><span class="p">[</span><span class="n">c</span><span class="p">]);</span> <span class="c1">// call internal function or macro</span>
            <span class="p">(</span><span class="o">*</span><span class="n">hist</span><span class="p">)[</span><span class="n">v</span><span class="p">]</span><span class="o">++</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="n">r</span> <span class="o">+=</span> <span class="n">sRowBytes</span><span class="p">;</span>
        <span class="n">g</span> <span class="o">+=</span> <span class="n">sRowBytes</span><span class="p">;</span>
        <span class="n">b</span> <span class="o">+=</span> <span class="n">sRowBytes</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Let&#8217;s consider the following issues of the implementation above:</p>
<ul class="simple">
<li>Works only for RGB (duplicate versions exist for other color spaces)</li>
<li>Works only for 8-bit images (duplicate versions exist)</li>
<li>Works only for planar images</li>
</ul>
</div>
<div class="section" id="gil-implementation">
<h2><a class="toc-backref" href="#id2">GIL implementation</a></h2>
<div class="highlight-cpp"><div class="highlight"><pre><span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">GrayView</span><span class="p">,</span> <span class="k">typename</span> <span class="n">R</span><span class="o">&gt;</span>
<span class="kt">void</span> <span class="n">grayimage_histogram</span><span class="p">(</span><span class="n">GrayView</span><span class="o">&amp;</span> <span class="n">img</span><span class="p">,</span> <span class="n">R</span><span class="o">&amp;</span> <span class="n">hist</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="k">typename</span> <span class="n">GrayView</span><span class="o">::</span><span class="n">iterator</span> <span class="n">it</span><span class="o">=</span><span class="n">img</span><span class="p">.</span><span class="n">begin</span><span class="p">();</span> <span class="n">it</span><span class="o">!=</span><span class="n">img</span><span class="p">.</span><span class="n">end</span><span class="p">();</span> <span class="o">++</span><span class="n">it</span><span class="p">)</span>
        <span class="o">++</span><span class="n">hist</span><span class="p">[</span><span class="o">*</span><span class="n">it</span><span class="p">];</span>
<span class="p">}</span>

<span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">View</span><span class="p">,</span> <span class="k">typename</span> <span class="n">R</span><span class="o">&gt;</span>
<span class="kt">void</span> <span class="n">luminosity8bit_hist</span><span class="p">(</span><span class="n">View</span><span class="o">&amp;</span> <span class="n">img</span><span class="p">,</span> <span class="n">R</span><span class="o">&amp;</span> <span class="n">hist</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">grayimage_histogram</span><span class="p">(</span><span class="n">color_converted_view</span><span class="o">&lt;</span><span class="n">gray8_pixel_t</span><span class="o">&gt;</span><span class="p">(</span><span class="n">img</span><span class="p">),</span><span class="n">hist</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Using the Boost.Lambda library (or C++11 lambda) features it can written
even simpler:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="k">using</span> <span class="n">boost</span><span class="o">::</span><span class="n">lambda</span><span class="p">;</span>

<span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">GrayView</span><span class="p">,</span> <span class="k">typename</span> <span class="n">R</span><span class="o">&gt;</span>
<span class="kt">void</span> <span class="n">grayimage_histogram</span><span class="p">(</span><span class="n">GrayView</span><span class="o">&amp;</span> <span class="n">img</span><span class="p">,</span> <span class="n">R</span><span class="o">&amp;</span> <span class="n">hist</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">for_each_pixel</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="o">++</span><span class="n">var</span><span class="p">(</span><span class="n">hist</span><span class="p">)[</span><span class="n">_1</span><span class="p">]);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Let&#8217;s consider the following advantages of the GIL version:</p>
<ul class="simple">
<li>Works with any supported channel depth, color space, channel ordering
(RGB vs BGR), and row alignment policy.</li>
<li>Works for both planar and interleaved images.</li>
<li>Works with new color spaces, channel depths and image types that can be
provided in future extensions of GIL</li>
<li>The second version is as efficient as the hand-coded version</li>
</ul>
<p>Shortly, it is also very flexible.</p>
<p>For example, to compute the histogram of the second channel of the top left
quadrant of the image, taking every other row and column, call:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="n">grayimage_histogram</span><span class="p">(</span>
    <span class="n">nth_channel_view</span><span class="p">(</span>
        <span class="n">subsampled_view</span><span class="p">(</span>
            <span class="n">subimage_view</span><span class="p">(</span><span class="n">img</span><span class="p">,</span>
                <span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span> <span class="n">img</span><span class="p">.</span><span class="n">width</span><span class="p">()</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">img</span><span class="p">.</span><span class="n">height</span><span class="p">()</span> <span class="o">/</span> <span class="mi">2</span><span class="p">),</span> <span class="c1">// upper left quadrant</span>
                <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span>                                     <span class="c1">// skip every other row and column</span>
            <span class="p">),</span>
        <span class="mi">1</span>   <span class="c1">// index of the second channel (for example, green for RGB)</span>
    <span class="p">),</span>
    <span class="n">hist</span>
<span class="p">);</span>
</pre></div>
</div>
<p>Since GIL operates on the source pixels of <code class="docutils literal"><span class="pre">img</span></code> object directly, no extra
memory is allocated and no images are copied.</p>
</div>
</div>


    <div class="navbar" style="text-align:right;">
      
      
      <a class="prev" title="Tutorial: Video Lecture" href="video.html"><img src="../_static/prev.png" alt="prev"/></a>
      <a class="next" title="Tutorial: Image Gradient" href="gradient.html"><img src="../_static/next.png" alt="next"/></a>
      
    </div>
    </div>
    <div class="footer" role="contentinfo">
      Last updated on 2021-08-05 09:08:10.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.6.
    </div>
  </body>
</html>